package org.little.jpa.controller;

import org.little.jpa.dao.UserRepository;
import org.little.jpa.domain.User;
import org.little.jpa.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by lenovo on 2017/3/17.
 */

@RestController
@RequestMapping(value = "/user/")
public class UserController {
    private static final Logger LOG = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private UserService userService;

    //使用get方式,一次性插入多条测试数据
    @RequestMapping(value = "inserUserForTest", method = RequestMethod.GET)
    public Object inserUserForTest(HttpServletRequest request) {
        userRepository.save(new User("AAA", 10));
        userRepository.save(new User("BBB", 20));
        userRepository.save(new User("CCC", 30));
        userRepository.save(new User("DDD", 40));
        userRepository.save(new User("EEE", 50));
        userRepository.save(new User("FFF", 60));
        userRepository.save(new User("GGG", 70));
        userRepository.save(new User("HHH", 80));
        userRepository.save(new User("III", 90));
        userRepository.save(new User("JJJ", 100));
        return null;
    }

    @RequestMapping(value = "findByName", method = RequestMethod.GET)
    public Object findByName(HttpServletRequest request) {
        String name = request.getParameter("name");
        User u = userRepository.findByName(name);
        return u;
    }

    //这里使用JdbcTemplate 直接执行sql,这里是耍流氓,不建议这么直接使用
    @RequestMapping(value = "findByName2", method = RequestMethod.GET)
    @Transactional
    public Object findByName2(HttpServletRequest request) {
//        String sql = "SELECT * from cont_album,cont_album_tag where cont_album.CP_ALBUM_ID=cont_album_tag.CP_ALBUM_ID and cont_album.CP_ID=cont_album_tag.CP_ID limit 50";
////        String name = request.getParameter("name");
////        User u = userRepository.findByName(name);
//        Map m = jdbcTemplate.queryForMap(sql);
//        return m;
        for (int i = 0; i < 10; i++) {
            String name = "";
            int age = 0;
            if (i == 5) {
//                name = "cesh111111111111111111i" + i;
                age = Integer.parseInt(name);
//                age = i;
            } else {
                name = "ceshi" + i;
                age = i;
            }

//            int age = i;
            LOG.info("insert into USER(NAME, AGE) values({}, {})", name, age);
            jdbcTemplate.update("insert into USER(NAME, AGE) values(?, ?)", name, age);

        }
        return null;
    }



    @RequestMapping(value = "testTransactional", method = RequestMethod.GET)
    public Object testTransactional(HttpServletRequest request) {
        userService.testInsertUser();
        return null;
    }

}
